package com.hfmall.service.impl;

import com.alibaba.fastjson.JSON;
import com.hfmall.common.GoodsVo;
import com.hfmall.entity.HfGoods;
import com.hfmall.entity.HfPictures;
import com.hfmall.entity.UtilGoodsVO;
import com.hfmall.mapper.HfGoodsMapper;
import com.hfmall.mapper.HfPicturesMapper;
import com.hfmall.service.IHfGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Service
public class HfGoodsServiceImpl extends ServiceImpl<HfGoodsMapper, HfGoods> implements IHfGoodsService {
    @Resource
    private HfGoodsMapper hfGoodsMapper;
    @Resource
    HfPicturesMapper hfPicturesMapper;

    @Override //根据共性表id查询商品表-再根据商品id查询图片套图
    public List<HfGoods> getHfGoodsByInfoId(int InfoId) {
        List<HfGoods> hfGoodsList = hfGoodsMapper.getHfGoodsByInfoId(InfoId);
        hfGoodsList.forEach(goods -> {
            if (goods != null || goods.getId() != null) {
                List<HfPictures> pictures = hfPicturesMapper.getHfPicturesByGoodsId(goods.getId());
                goods.setHfPictureList(pictures);
            }
        });
        return hfGoodsList;
    }

    @Override
    public List<UtilGoodsVO> getInfoById(int id) {
        return hfGoodsMapper.getById(id);
    }

    @Override
    public List<GoodsVo> getSkuById(Integer id) {

        List<GoodsVo> goodsVos = hfGoodsMapper.getByGoodsId(id);

        List<GoodsVo> newGoodsVos = new ArrayList<>();
        for (int i = 0; i < goodsVos.size(); i += 2) {
            String value = goodsVos.get(i).getValue();
            String newValue = (String) JSON.toJSON(value + ";" + goodsVos.get(i + 1).getValue());
            goodsVos.get(i).setValue(newValue);

            String className = goodsVos.get(i).getClassName();
            String newName = (String) JSON.toJSON(className + ";" + goodsVos.get(i + 1).getClassName());
            goodsVos.get(i).setClassName(newName);

            newGoodsVos.add(goodsVos.get(i));
        }
        return newGoodsVos;
    }
}
